import React from 'react'
import './css/table-menu.css'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const confirm 				= $Lazy.file(()=>import('@antd/confirm'))
const CopyModal			= $Lazy.state(()=>import('./copy-modal'))
const MoveModal			= $Lazy.state(()=>import('./move-modal'))
const UploadFileModal	= $Lazy.state(()=>import('./upload-file-modal'))
const EffectiveTimeModal	= $Lazy.state(()=>import('./effective-time-modal'))
const LevelSettingModal	= $Lazy.state(()=>import('./level-setting-modal'))
const RenameModal	= $Lazy.state(()=>import('./rename-modal'))
// ===================================================================== data
// ===================================================================== component
class Index extends React.Component{
	state={
		data:[],
		style:{},
		display:'none',
		isMore:false,
		showEdit:false,
		showEmpower:false,
		share: false
	}
	
	open = (e,rows,id) => {
		this.id = id ?? false
		let { showEdit, showEmpower, folder, share } = this.props
		showEdit = showEdit?showEdit:false
		share = share?share:false
		showEmpower = showEmpower?showEmpower:false
		folder = folder?folder:false
		this.rows = rows
		this.uuids = rows.map(v=>v.uuid)
		let menuDivH
		let isMore
		if(rows.length===1){
			isMore=false
			menuDivH = showEdit?(share?180:300):60
		}else{
			isMore=true
			menuDivH = showEdit?(share?90:210):30
		}
		this.setState({isMore,showEdit,showEmpower,folder, share})
		const { clientX, clientY } = e
		const { innerHeight } = window
		let style
		if(clientY+menuDivH>innerHeight){
			style = {
				top: clientY-40-(clientY+menuDivH-innerHeight)-10,
				left: clientX-300,
			}
		}else{
			style = {
				top: clientY-40,
				left: clientX-300,
			}
		}
		this.setState({style, display: 'block'})
	}
	isHide = (boolean) => {
		this.setState({ display: boolean?'none':'block'})
	}
	onRefresh = ()=> this.props.onRefresh()
	
	power = (data) => {		// 判断权限
		let boolean = false
		if($fn.hasArray(this.rows)){
			let arr = []
			this.rows.forEach(v=>{
				const { perm_codes } = v
				if($fn.hasArray(perm_codes)){
					perm_codes.forEach(v1=>{
						if(v1.indexOf(data) !== -1){
							arr.push(v)
						}
					})
				}
			})
			if($fn.hasArray(arr)){
				this.uuid = arr.map(v=>v.uuid)
				boolean = true
			}
		}
		return boolean
	}
	
	download = () =>{		// 下载
		if(this.state.share){
			if(this.power('download')){
				$http.submit(null,'doc-file-list/download',{param:{uuids:this.uuids}}).then((data)=>{
					data.forEach(v=>window.open(v))
					$fn.msg.success('文件下载成功')
				})
			}else{
				setTimeout(()=>$fn.msg.warning('您没有下载权限！'))
			}
		}else{
			$http.submit(null,'doc-file-list/download',{param:{uuids:this.uuids}}).then((data)=>{
				data.forEach(v=>window.open(v))
				$fn.msg.success('文件下载成功')
			})
		}
	}
	
	delete = () => {		// 删除
		if(this.state.share){
			if(this.power('del')){
				this.deleteCon()
			}else{
				setTimeout(()=>$fn.msg.warning('您没有删除权限！'))
			}
		}else{
			this.deleteCon()
		}
	}
	deleteCon = () => {		// 删除文件
		confirm.then( f => {
			f({
				msg:'是否删除选中文件？',
				onOk: close => {
					$http.submit(null,'doc-file-list/del',{param:{uuids:this.uuids}}).then((data)=>{
						close()
						$fn.msg.success('文件删除成功')
						this.props.onRefresh()
					})
				}
			})
		})
	}
	openFile = (isEdit) => {		// 打开、编辑文件
		const { showEdit, showEmpower, folder } = this.state
		const baseUrl = $fn.getRoot().root+'/workbench/document-manage'
		if(this.state.share){
			if(isEdit){
				if(this.power('save')){
					this.props.history.push(baseUrl+`/file-detail?uuid=${this.uuids[0]}&isEdit=${isEdit}&showEdit=${true}&showEmpower=${showEmpower}&folder=${folder}`)
				}else{
					setTimeout(()=>$fn.msg.warning('您没有编辑权限！'))
				}
			}else{
				if(this.power('edit')){
					this.props.history.push(baseUrl+`/file-detail?uuid=${this.uuids[0]}&isEdit=${isEdit}&showEdit=${false}&showEmpower=${showEmpower}&folder=${folder}`)
				}else{
					setTimeout(()=>$fn.msg.warning('您没有预览权限！'))
				}
			}
		}else{
			this.props.history.push(baseUrl+`/file-detail?uuid=${this.uuids[0]}&isEdit=${isEdit}&showEdit=${showEdit}&showEmpower=${showEmpower}&folder=${folder}`)
		}
		
		// window.folderType = {uuid:this.uuids[0],isEdit, showEdit, showEmpower, folder}
		// window.updateFolder()
	}
	copy = ()=>{		// 复制
		if(this.state.share){
			if(this.power('copy')){
				this.copyModal.open(this.uuids)
			}else{
				setTimeout(()=>$fn.msg.warning('您没有复制权限！'))
			}
		}else {
			this.copyModal.open(this.uuids)
		}
	}
	rename = () => {		// 重命名
		if(this.state.share){
			if(this.power('rename')){
				this.renameModal.open(this.rows[0])
			}else{
				setTimeout(()=>$fn.msg.warning('您没有重命名权限！'))
			}
		}else{
			this.renameModal.open(this.rows[0])
		}
	}
	
	render(){
		const { style, display,isMore, showEdit, share } = this.state
		return (
			<>
				<div className='abs_full' style={{display}} onClick={()=>this.isHide(true)} >
					<div className='abs bcb box' id='table-menu' style={{...style}}>
						<ul className='menu'>
							{!isMore && <li onClick={()=>this.openFile(false)} >打开</li>}
							{!isMore && showEdit && <li onClick={()=>this.openFile(true)}>编辑</li>}
							<li onClick={this.download}>下载</li>
							{showEdit && <li onClick={this.delete}>删除</li>}
							{showEdit && <li onClick={this.copy}>复制</li>}
							{showEdit && !share && <li onClick={()=>this.moveModal.open(this.uuids)}>移动</li>}
							{showEdit && !share &&  <li onClick={()=>this.uploadFileModal.open(this.id)}>上传</li>}
							{!isMore && showEdit && <li onClick={this.rename}>重命名</li>}
							{showEdit && !share &&  <li onClick={()=>this.effectiveTimeModal.open()}>有效时效</li>}
							{showEdit && !share &&  <li onClick={()=>this.levelSettingModal.open()}>等级设置</li>}
						</ul>
					</div>
				</div>
				{/* 复制 */}
				<CopyModal onRef={ref=>this.copyModal=ref} onRefresh={this.onRefresh} />
				{/* 移动 */}
				<MoveModal onRef={ref=>this.moveModal=ref} onRefresh={this.onRefresh} />
				{/* 上传文件 */}
				<UploadFileModal onRef={ref=>this.uploadFileModal=ref} onRefresh={this.onRefresh} />
				{/* 有效时效 */}
				<EffectiveTimeModal onRef={ref=>this.effectiveTimeModal=ref} />
				{/* 等级设置 */}
				<LevelSettingModal onRef={ref=>this.levelSettingModal=ref} />
				{/* 重命名 */}
				<RenameModal onRef={ref=>this.renameModal=ref} onRefresh={this.onRefresh} />
			</>
		)
	}
}
export default Index